package com.hospital.pony.fragment;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.http.AjaxParams;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.hospital.MyDeportActivity;
import com.hospital.R;
import com.hospital.pony.base.BaseFragment;
import com.hospital.pony.config.Consts;
import com.hospital.pony.model.HospitalModel;
import com.hospital.pony.model.ResponseEntry;
import com.hospital.pony.util.ToastUtil;
import com.hospital.pony.adapter.LookListAdapter;


public class HospitalFragment extends BaseFragment {

	// 获取view
	private View rootView;
	// 获取控件
	private ListView mListMessage;
	private List<HospitalModel> listMsg = new ArrayList<HospitalModel>();


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_news, null);
		initWidget();
		initData();
		return rootView;
	}

	@Override
	public void initWidget() {
		
		
		mListMessage = (ListView) rootView.findViewById(R.id.mListMessage);
		
		mListMessage.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

				Intent playMusic = new Intent(getActivity(), MyDeportActivity.class);
				playMusic.putExtra("msg",listMsg.get(position));// 序列化,要注意转化(Serializable)
				startActivity(playMusic);

			}
		});
		//

	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public void initData() {

		listHopitalPhoenMessage(true);


	}


	private void listHopitalPhoenMessage(boolean isShow) {
		AjaxParams params = new AjaxParams();
		params.put("action_flag", "listHopitalPhoenMessage");
		httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultCode, isShow, "正在加载...");
	}


	@Override
	protected void callBackSuccess(ResponseEntry entry, int actionId) {
		super.callBackSuccess(entry, actionId);

		switch (actionId) {
		case Consts.actionId.resultCode:
			if (null != entry.getData() && !TextUtils.isEmpty(entry.getData())) {

				String jsonMsg = entry.getData().substring(1, entry.getData().length() - 1);
				if (null != jsonMsg && !TextUtils.isEmpty(jsonMsg)) {
					listMsg.clear();
					listMsg = mGson.fromJson(entry.getData(), new TypeToken<List<HospitalModel>>() {
					}.getType());
					LookListAdapter	musicNetListAdapter = new LookListAdapter(getActivity(), listMsg);
					mListMessage.setAdapter(musicNetListAdapter);
				}
			}
			break;

		}

	}

	@Override
	protected void callBackAllFailure(String strMsg, int actionId) {
		super.callBackAllFailure(strMsg, actionId);
		ToastUtil.show(getActivity(), strMsg);

	}



}
